package com.atguigu.lease.web.admin.controller.attr;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.vo.AttrKeyVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/attr")
@Tag(name = "房间属性管理模块")
public class AttrKeyController {
    @Resource
    AttrKeyService attrKeyService;
    /*
        设计接口：
            1、前端提交的参数如何接收
                分析前端提交的参数 后端是否有实体类可以直接接收
                如果没有：新创建实体类接收   dto(data transfer object)
                如果有 直接使用
            2、后端响应的结果如何按照前端的要求封装
                如果没有：新创建实体类组装  vo( view object )
                    AttrKey: id  name
                    前端需要：  id  name  attrValueList( id  name  attrKeyId)
                        AttrKey: 可以基于现有类扩展、也可以创建新的类
                            id
                            name
                            List<AttrValue> attrValueList
                如果有 直接使用
     */
    @Operation(summary = "查询所有属性和属性值集合")
    @GetMapping("list")
    public Result<List<AttrKeyVo>> list(){

        return Result.ok(attrKeyService.listAttrKeyVos());
    }
    //admin/attr/key/saveOrUpdate
    //saveOrUpdate
    @Operation(summary = "新增/更新房间属性")
    @PostMapping("/key/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody AttrKey attrKey){
//select t1.id, t1.name , t2.id avId , t2.name avName from attr_key t1 left join attr_value t2
// on t1.id = t2.attr_key_id and t2.is_deleted = 0  where t1.is_deleted = 0
        //sql执行的顺序： from join on  where
        return Result.build(attrKeyService.saveOrUpdateAttrKey(attrKey));
    }
    ///key/deleteById?attrKeyId=1829410313560748034
    @Operation(summary = "删除房间属性 和 值")
    @DeleteMapping("/key/deleteById")
    public Result deleteById(@RequestParam("attrKeyId")Long attrKeyId){
        return Result.build(attrKeyService.deleteById(attrKeyId));
    }
}
